// pages/main/main.js
import {
  getBanner,
  getArticle,
  getTopArticle,
  collectInnerArticle,
  cancelCollectArticle
} from '../../api/main'
const app = getApp()
Page({

  /**
   * Page initial data
   */
  data: {
    bannerList: [],
    articleList: [],
    topList: [],
    currentPageIndex: 0,
    isRefresh: false,
    hasLogin: app.globalData.hasLogined
  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad: function (options) {
    wx.hideHomeButton({
      success: (res) => {

      },
    })
    getBanner().then((res) => {
      if (res) {
        this.setData({
          bannerList: res.data
        })
      }

    })
    getTopArticle().then((res) => {
      let data = res.data
      let tempList = []
      for (let i = 0; i < data.length; i++) {
        let x = data[i]
        x['isTop'] = true
        tempList.push(x)
      }
      const {
        articleList
      } = this.data
      this.setData({
        topList: tempList
      }, () => {
        this.getArticleList('refresh', 0)
      })
    })
  },
  /**
   * 请求文章
   * @param {*} type refresh,loadMore
   * @param {*} pageIndex 页码从0开始
   */
  getArticleList(type, pageIndex) {
    const {
      articleList,
      topList
    } = this.data
    getArticle(pageIndex).then((res) => {
      if (res) {
        let data = res.data
        if (type === 'refresh') {
          if (topList.length > 0) {
            let tempX = []
            tempX = data.datas
            let xx = topList.concat(tempX)
            this.setData({
              articleList: xx,
              currentPageIndex: pageIndex,
              isRefresh: false
            })
          } else {
            this.setData({
              articleList: data.datas,
              currentPageIndex: pageIndex,
              isRefresh: false
            })
          }
          // if()
        } else {
          if (data.total > articleList.length - topList.length && data.datas) {
            let x = articleList.concat(data.datas)
            this.setData({
              articleList: x,
              currentPageIndex: pageIndex
            })
          } else {
            wx.showToast({
              title: '没有更多数据了',
              icon:'none'
            })
          }
        }
      }
    })

  },
  loadMore: function () {
    const {
      currentPageIndex
    } = this.data
    let pageIndex = currentPageIndex
    this.getArticleList('loadMore', ++pageIndex)
  },
  refresh: function (e) {
    this.setData({
      isRefresh: true
    })
    this.getArticleList('refresh', 0)
  },
  collect: function (e) {
    console.log('e=', e)
    this.setData({
      hasLogin: app.globalData.hasLogined
    })
    const {
      hasLogin,
      articleList
    } = this.data
    const {
      id,
      index,
    } = e.currentTarget.dataset
    if (hasLogin) {
      let x = articleList[index].collect
      console.log('x',x)
      if (!x) {
        collectInnerArticle(id).then((res) => {
          console.log(res)
          if (res) {
            articleList[index].collect = true
            this.setData({
              articleList:articleList
            })
            wx.showToast({
              title: '收藏成功',
            })
          }else{
          }
        })
      } else {
        cancelCollectArticle(id).then((res) => {
          console.log(res)
          if (res) {
            articleList[index].collect = false
            this.setData({
              articleList:articleList
            })
            wx.showToast({
              title: '取消收藏',
              icon:''
            })
          }
        })
      }

    } else {
      wx.showToast({
        title: '请先登录',
      })
      wx.navigateTo({
        url: '../login/login',
        success: function (res) {
          console.log('resx', res)
        }
      })
    }

  },
  showDetail: function (e) {
    console.log('e', e)
    const {
      link
    } = e.currentTarget.dataset
    const url = encodeURIComponent(JSON.stringify(link));
    wx.navigateTo({
      url: `../webview/webview?url=${url}`,
    })
  },
  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady: function () {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    // wx.hideHomeButton({
    //   success: (res) => {

    //   },
    // })
  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function () {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function () {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh: function () {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom: function () {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function () {

  }
})